<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1.函数编程与面向对象的实例对比</title>
</head>
<body>
  <script>
   // 函数编程 （面向过程编程）
   // 函数会挂载到window上，如果再创建重复名称的函数会有问题
   let name = "向军";
    let grade = [
      { lesson: "js", score: 99 },
      { lesson: "mysql", score: 85 }
    ];
    function average(grade, name) {
      const total = grade.reduce((t, a) => t + a.score, 0);
      return name + ":" + total / grade.length + "分";
    }
    console.log(average(grade, name));
    // 面向对象 字面量创建对象
    // 更清晰，也减少了函数的参数传递，也不用担心函数名的覆盖
    let user = {
        name: "后盾人",
        grade: [
          { lesson: "js", score: 99 },
          { lesson: "mysql", score: 85 }
        ],
        average() {
          const total = this.grade.reduce((t, a) => t + a.score, 0);
          return this.name + ":" + total / grade.length + "分";
        }
      };
      console.log(user.average());
  </script>
</body>
</html>